from django.shortcuts import render
from django.views import View

from .models import ContentCategory
from .utils import get_categories


# Create your views here.
class IndexView(View):
    """首页广告"""

    def get(self, request):
        """提供首页广告的页面"""

        categories = get_categories()
        # 展示首页广告页面
        # 查询首页广告数据
        contents = {}
        content_categories = ContentCategory.objects.all()

        # 查询所有的广告类型
        for content_category in content_categories:
            # 使用广告类别查询出该类别所对象的广告内容
            contents[content_category.key] = content_category.content_set.filter(status=True).order_by('sequence')

        # 构造上下文
        context = {
            'contents': contents,
            "categories": categories,
        }

        return render(request, 'index.html', context)
